<?php
require_once(ABSPATH. 'include/dao/mydao.php');
require_once(ABSPATH. 'include/model/Post.php');

class PostDAO{
	function insert(&$vo){
		$this->cc($vo);
		$sql = "insert into posts(user_id,category_id,title,keywords,description,body,post_date,last_update_date,last_reply_date,reply_count,status)values(".
	     "'".$vo->user_id."',".
	     "'".$vo->category_id."',".
	     "'".$vo->title."',".
	     "'".$vo->keywords."',".
	     "'".$vo->description."',".
	     "'".$vo->body."',".
	     "now(),".
	     "now(),".
	     "now(),".
	     "'0',".
	     "'0')";
		$vo-> id = mydao_insert($sql);
	}

	function update(&$vo){
		$this->cc($vo);
		$sql = "update posts set ".
	     "category_id='".$vo->category_id. "',".
	     "title='".$vo->title. "',".
	     "keywords='".$vo->keywords. "',".
	     "description='".$vo->description. "',".
	     "body='".$vo->body. "',".
	     "last_update_date=now(),".
	     "status='".$vo->status. "'  ".
	     "where id=".$vo->id ;	
		
		mydao_runSql($sql);
	}

	function set_status($id,$status){
		$sql="update posts set status='".$status."' where id='".$id."'";
		mydao_runSql($sql);
	}

	function get($id){
		$sql="select posts.id,user_id,category_id,title,keywords,description,body,post_date,posts.last_update_date,posts.last_reply_date,posts.reply_count,posts.status".
		" ,users.name,users.profile_image_url ".
		" from posts, users ".
		" where posts.user_id=users.id and posts.id=".$id ;
		return $this->convert(mydao_getLine($sql));
	}

	function get_categoryId($categoryId,$offset){
		$sql="select posts.id,user_id,category_id,title,keywords,description,body,post_date,posts.last_update_date,last_reply_date,reply_count,posts.status".
		" ,users.name,users.profile_image_url ".
		" from posts,users ".
		" where posts.user_id=users.id and category_id=$categoryId" .
		" order by posts.id desc ".
		" LIMIT $offset,15";
		return $this-> convertRows(mydao_getData($sql));
	}
	
	function get_userId($userId,$offset){
		$sql="select id,user_id,category_id,title,keywords,description,body,post_date,last_update_date,last_reply_date,reply_count,status".
		" from posts where user_id=$userId" .
		" order by id desc ".
		" LIMIT $offset,15";
		return $this-> convertRows(mydao_getData($sql));
	}
	function getUserRecordsCount($user_id){
		$sql = "SELECT COUNT(*) FROM posts where user_id='". $user_id."'";
		return mydao_getVar($sql);
	}
	
	function getRecordsCount($categoryId){
		$sql='';
		if($categoryId){
			$sql ="SELECT COUNT(*) FROM posts where category_id='". $categoryId."'";
		}else{
			$sql ="SELECT COUNT(*) FROM posts";
		}
		return mydao_getVar($sql);
	}
	
	
    function get_all($offset){
		$sql="select posts.id,user_id,category_id,title,keywords,description,body,post_date,posts.last_update_date,posts.last_reply_date,posts.reply_count,posts.status".
		" ,users.name,users.profile_image_url ".
		" from posts, users ".
		" where posts.user_id=users.id " .
		" order by id desc ".
		" LIMIT $offset,15";
    	return $this-> convertRows(mydao_getData($sql));
	}

	private function cc(&$vo){
		$mysql = new SaeMysql();
		$vo->user_id = intval($vo->user_id);
		$vo->category_id = intval($vo->category_id);
		$vo->title = $mysql-> escape(strip_tags($vo->title));
		$vo->keywords = $mysql-> escape(strip_tags($vo->keywords));
		$vo->description = $mysql-> escape(strip_tags($vo->description));
		$vo->body = $mysql-> escape(strip_tags($vo->body));
	}

	private function convert($result){
		$vo = new Post();
		$vo->id = $result["id"];		
		$vo->title = $result["title"];
		$vo->keywords = $result["keywords"];
		$vo->description = $result["description"];
		$vo->body = $result["body"];
		$vo->post_date = $result["post_date"];
		$vo->last_update_date = $result["last_update_date"];
		$vo->last_reply_date = $result["last_reply_date"];
		$vo->reply_count = $result["reply_count"];
		$vo->status = $result["status"];
		
		$vo->user_id = $result["user_id"];
		$vo-> User->id=$result["user_id"];
		$vo-> User->name=$result["name"];
		$vo-> User->profile_image_url=$result["profile_image_url"] ;
		
		$vo->category_id = $result["category_id"];
		
		return $vo;
	}

	private function convertRows($rows){
		if(!$rows) return false;
		
		$results = array();
		foreach ($rows as $row){
			array_push($results, $this->convert($row));
		}
		return $results;
	}
}
?>